"use strict"
const Controller = require("egg").Controller
const path = require("path")
const fs = require("fs")
const {sendStat} = require("../extend/helper")
let start = Date.now()
let hasOwnProperty = Object.prototype.hasOwnProperty

class ServerController extends Controller {
	constructor(ctx) {
		super(ctx)
		this.uploadPath = this.app.config.serverConfig.uploadPath
		this.imgBase = this.app.config.serverConfig.imgBase
		this.constant = this.config.constant
	}
	async index() {
		this.ctx.body = "hi, egg"
	}
	async freshCache() {

		if (this.ctx.query.key && hasOwnProperty.call(this.app.cache, this.ctx.query.key)) {
			this.app.cache[this.ctx.query.key] = undefined
			this.ctx.body = await this.app.cache[this.ctx.query.key]
			return
		}
		this.app.cache.ads = undefined
		this.app.cache.application = undefined
		this.app.cache.moduleApplicat = undefined
		this.app.cache.empireStore = undefined
		this.app.cache.additionProp = undefined
		this.app.cache.tskList = undefined
		this.ctx.body = "清除成功"
	}

	async uploadImg() {
		let {ctx} = this
		let fileObj = {}
		let body = Object.assign({}, this.ctx.req.body)
		ctx.validate({type: ["share", "ad", "key"]}, body)
		this.ctx.req.files.forEach((val) => {
			fileObj[val.fieldname] = val
		})
		if (!Array.isArray(ctx.req.files) || ctx.req.files.length != 1 || fileObj.img == undefined) {
			if (body.edit == 0)// 创建时候没有上传图片不允许
				ctx.throw(422, "no has file", {
					code: "invalid_param",
					errors: [{"message": "创建时候必须上传图片", "field": "img", "code": "missing_file"}],
				})
		}
		let fileName = fileObj.img.filename + "." + fileObj.img.mimetype.split("/").pop()

		let mvPath = path.resolve(path.join(this.uploadPath, body.type, fileName))
		let basePath = path.join(this.imgBase, body.type, fileName)
		if (!mvPath.startsWith(this.uploadPath)) {
			ctx.body = sendStat(this.constant.ERROR_UPLOAD_FAIL, {stat: false})
			return
		}// 验证图片路径是否合法
		let uploadRes = await new Promise(function (reslove, _reject) {
			fs.rename(fileObj.img.path, mvPath, function (errors) {
				reslove({stat: !errors})
			})
		})
		ctx.body = sendStat(uploadRes.stat ? this.constant.NORMAL : this.constant.ERROR_UPLOAD_FAIL, {...uploadRes, path: basePath})
	}

	async sysInfo() {
		this.ctx.body = sendStat(this.constant.NORMAL, {
			now: Date.now(), start, db: this.app.config.sequelize.database
		})
	}

	async delRedis() {
		this.ctx.validate({key: "string"}, this.ctx.query)
		let content = await this.app.redis.get(this.ctx.query.key)
		await this.app.redis.del(this.ctx.query.key)
		this.ctx.body = sendStat(this.constant.NORMAL, content)
	}
	async getRedis() {
		this.ctx.validate({key: "string"}, this.ctx.query)
		let content = await this.app.redis.get(this.ctx.query.key)
		this.ctx.body = sendStat(this.constant.NORMAL, content)
	}
	async redisKeys() {
		this.ctx.validate({key: "string"}, this.ctx.query)
		let content = await this.app.redis.keys(this.ctx.query.key)
		this.ctx.body = sendStat(this.constant.NORMAL, content)
	}

	async test() {
		return new Promise((reslove) => {
			setTimeout(() => {
				reslove({})
				this.ctx.body = Date.now()
			}, 4000)
		})
	}
}

module.exports = ServerController
